<!-- 项目审批 -->
<template>
	<view style="margin-bottom: 100rpx;">
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">入库计划</block></cu-custom>
		<view>
		<uni-nav-bar :fixed="true" color="#333333" background-color="#4364F7" :border="false">
			<view class="input-view">
				<uni-icons type="search" size="22" color="#666666" />
				<input v-model="searchVal" confirm-type="search" class="input" type="text" placeholder="单号/批次" @confirm="search">
				<uni-icons :color="'#999999'" v-if="searchVal!==''" class="icon-clear" type="clear" size="22" @click="clear" />
			</view>
			<!-- <uni-search-bar style="width: 100%;" placeholder="搜索项目/机构" radius="100" clearButton="auto" @confirm="search" /> -->
		</uni-nav-bar>
		<mescroll-uni :down="downOption" @emptyclick="downCallback" @down="downCallback" :up="upOption" @up="upCallback"
		 :fixed="true" :top="90">
			<view :style="{ paddingTop: customBarH+'px' }" class="mycard">
				<view v-for="item in cardList" :key="item.id" class="card-item">
					<uni-card is-full title="入库计划" is-shadow note="true" :extra="formatDate(item.dat)" thumbnail="/static/img/入库计划.svg">
						<view class="audit-card-content">
							<view class="uni-flex uni-row">
								<view class="flex-item-20">单号</view>
								<view class="flex-item-80">{{item.id}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-20">入库计划编号</view>
								<view class="flex-item-80">{{item.code}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-20">客户</view>
								<view class="flex-item-80">{{item.client.name}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-20">到货方式</view>
								<view class="flex-item-80">{{item.arriveType}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-20">车牌号</view>
								<view class="flex-item-80">{{item.vehicleNum}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-20">司机</view>
								<view class="flex-item-80">{{item.driverName}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-20">状态</view>
								<view class="flex-item-80">
									<uni-tag size="small" :text="formatStatus(item.status).text" :type="formatStatus(item.status).color"
									 :circle="true"></uni-tag>
								</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-20">操作员</view>
								<view class="flex-item-80">{{item.oper}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-20">操作时间</view>
								<view class="flex-item-80">{{formatDate(item.date)}}</view>
							</view>
							<view class="uni-flex uni-row">
								<view class="flex-item-20">备注</view>
								<view class="flex-item-80">{{item.remark}}</view>
							</view>
						</view>

						<template v-slot:footer>
							<view class="footer-box">
								<view class="text-green" ></view>
								<view class="cuIcon-form text-blue" @tap="detailClick(item)">明细</view>
							</view>
						</template>
					</uni-card>
				</view>
			</view>
		</mescroll-uni>
		</view>
	</view>
</template>

<script>
	import MescrollUni from "@/components/mescroll-uni/mescroll-uni.vue"
	import uniCard from "@/components/uni-card/uni-card"
	import uniTag from '@/components/uni-tag/uni-tag.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'

	import {
		mapGetters
	} from 'vuex'
	import {
		getRandomArrayElements,
		filePreview,
		formatStatus
	} from '@/utils/index.js'
	import { formatDate } from '@/utils/datetime.js'
	export default {
		components: {
			MescrollUni,
			uniCard,
			uniTag,
			uniIcons
		},
		computed: mapGetters(['loginType','user']),
		data() {
			return {
				mescroll: null,
				isPass: false,
				selectedProject: {},
				// 下拉刷新的常用配置
				downOption: {
					use: true, // 是否启用下拉刷新; 默认true
					auto: true, // 是否在初始化完毕之后自动执行下拉刷新的回调; 默认true
				},
				// 上拉加载的常用配置
				upOption: {
					use: true, // 是否启用上拉加载; 默认true
					auto: true, // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					},
					toTop: {
						src: '/static/img/backtop.png'
					},
					textNoMore: '没有更多数据了'
				},
				// 列表数据
				cardList: [],
				datas: [],
				searchVal: '',
				customBarH:this.CustomBar
			}
		},
		methods: {
			filePreview,
			formatStatus,
			formatDate,
			// mescroll组件初始化的回调,可获取到mescroll对象
			mescrollInit(mescroll) {
				// 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
				//this.mescroll = mescroll
			},
			/*下拉刷新的回调, 有三种处理方式: */
			downCallback(mescroll) {
				// 第2种: 下拉刷新和上拉加载调同样的接口, 那以上请求可删, 直接用mescroll.resetUpScroll()代替
				mescroll.resetUpScroll() // 重置列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			},
			/*上拉加载的回调*/
			upCallback(mescroll) {

				/**
				 * todo: api请求分页查询项目待办
				 */

					if (this.loginType === '司机') {

						const params = {
							driverName: this.user.name,
							driverPhone: this.user.phone
						  }
						this.$minApi.getGoodsInPlanTodoList(params).then(res => {
						
							const data = res
							if (data) {
								this.cardList = data
							}
						}).catch(err => {
							console.log(err)
						}).finally(() => {
							mescroll.endErr()
						})
						
					} else {

						let pageNum = mescroll.num // 页码, 默认从1开始
						let pageSize = mescroll.size // 页长, 默认每页10条
						let goodsName = this.searchVal
						if (this.goodsName) {
							// goodsName = this.goodsName
						}
						const params = {
							filter: this.searchVal,
							pageIndex: pageNum,
							pageSize: pageSize,
						  }						 
						 
						this.$minApi.getGoodsInPlans(params).then(res => {
						
							const data = res
							if (data) {
								
								let totalPage = data.totalCount
								let hasNext = this.cardList.length < totalPage
								
								if (mescroll.num === 1) this.cardList = [] //如果是第一页需手动置空列表
								this.cardList = this.cardList.concat(data.list) //追加新数据
							
								// 成功隐藏下拉加载状态
								// 方法一(推荐): 后台接口有返回列表的总页数 totalPage
								mescroll.endByPage(data.list.length, totalPage)
								this.$nextTick(() => {
									mescroll.endSuccess(data.list.length)
								})					
								
							}
						}).catch(err => {
							console.log(err)
						}).finally(() => {
							mescroll.endErr()
						})
					}
					
			},
			detailClick(item) {
				uni.redirectTo({
					url: '/pages/goodsInPlan/detail?id=' + item.id
				})
			},
			search() {
				
				if (this.loginType === '司机') {
					
					if (this.searchVal) {
						this.cardList = this.datas.filter(f => f.id === Number(this.searchVal) || f.code === Number(this.searchVal))
					} else {
						this.cardList = this.datas
					}
					
				} else {
					
					let pageNum = 1 // 页码, 默认从1开始
					let pageSize = 10 // 页长, 默认每页10条

					const params = {
						filter: this.searchVal,
						pageIndex: pageNum,
						pageSize: pageSize,
					  }	
										   
				   uni.showLoading({
					title: '正在查询数据...'
				   })

					this.$minApi.getGoodsInPlans(params).then(res => {
					
						const data = res
						if (data) {
							
							let totalPage = data.totalCount
							let hasNext = this.cardList.length < totalPage
							
							this.cardList = [] //如果是第一页需手动置空列表
							this.cardList = this.cardList.concat(data.list) //追加新数据			
							
						}
					}).catch(err => {
						console.log(err)
					}).finally(() => {
						// 隐藏加载状态
						uni.hideLoading()
					})
				}
				
			},
			
			clear() {
				this.searchVal = ''
				this.search()
			},
			backedPage() {
				uni.switchTab({
					url: '/pages/main/index'
				})
			}
		}
	}
</script>

<style>
	.mescroll-totop {
		opacity: 1 !important;
	}
</style>
